﻿package com.skillscoach {
	import com.senocular.display.TransformTool;
	import com.senocular.display.transform.*;
	
	import fl.motion.Color;
	import fl.video.FLVPlayback;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	public class ToolsPanel extends MovieClip{

		public var videoHolder:MovieClip;
		public var videoClip:MovieClip;
		public var frameChild:MovieClip;
		public var curDrawingTool:MovieClip;
		private var colorNormal:Color = new Color();
		private var colorOver:Color = new Color();
		private var panel:MovieClip = this as MovieClip;
		public var clickFirst:Boolean = false;
		public var childIndex:Number = 0;
		public var valX:Number;
		public var valY:Number;
		public var currentToolSelected:String = "none";
		private var buttonTypeArray:Array = new Array("","","","","","","","pushButton","pushButton","pushButton","pushButton","");
		private var videoZoomLevel:uint = 0;
		private var newFormat:TextFormat = new TextFormat();
		private var drawingColor:Number;
		private var textToolSelected:Boolean = false;


		public var drawMode:String;
		private var deleteTool:MovieClip; 
		
		public var childMc:Shape;
		public var childHolder:Sprite;
		public var currChild:Sprite;
		
		private var tool:TransformTool = new TransformTool(new ControlSetStandard());
		
		
		public function ToolsPanel() {
			// constructor code
			
			//
			colorNormal.setTint(0x2d2d2d,1);
			colorOver.setTint(0xffffff,1);
			//loadTools();		
			stage.addEventListener(MouseEvent.MOUSE_DOWN, tool.deselect);
			
		}

		public function setTools():void
		{
			for(var i:int = 1; i<15; i++)
			{
				
				var tool:MovieClip = panel.getChildByName("tool"+i) as MovieClip;
				tool.icons.gotoAndStop(i);
				tool.buttonType = buttonTypeArray[i-1];
				tool.mouseChildren = false;
				tool.icons.transform.colorTransform = colorNormal;

				tool.buttonMode = true;
				tool.addEventListener(MouseEvent.ROLL_OVER, toolRollOver);
				tool.addEventListener(MouseEvent.ROLL_OUT, toolRollOut);
				if(i < 8)
				{
					tool.addEventListener(MouseEvent.CLICK, drawToolClicked);
				}else {
					tool.addEventListener(MouseEvent.CLICK, toolClicked);
				}
			}
			
			deleteTool= panel.getChildByName("tool13") as MovieClip;
			deleteTool.mouseEnabled = false;
			deleteTool.mouseChildren = false;
			deleteTool.alpha = .3;
			deleteTool.addEventListener(MouseEvent.CLICK,deleteFn)
	
		}
		public function loadTools(vHolder:MovieClip, vClip:MovieClip):void
		{
			
			videoHolder = vHolder;
			videoClip = vClip;				
			videoHolder.addEventListener(MouseEvent.MOUSE_DOWN,mDown);
			
		}
		public function createChild():void
		{
			frameChild = new MovieClip();
			videoHolder.addChild(frameChild);
		}
		private function deleteFn(e:MouseEvent):void
		{
			var mc:MovieClip = MovieClip(tool.target);
			if(mc)
			{
				tool.setTarget(null);
				mc.parent.removeChild(mc);
			}
				
		}
		
		private function toolRollOver(e:MouseEvent):void
		{
			e.target.gotoAndStop(2);
		}
		private function toolRollOut(e:MouseEvent):void
		{
			if(e.target != curDrawingTool)
			{
				e.target.gotoAndStop(1);
			}
			
		}
		
		public function mDown(e:MouseEvent):void
		{
			drawingColor = picker.selectedColor;
			if(textToolSelected)
			{
				addText();
			}
			videoHolder.addEventListener(MouseEvent.MOUSE_MOVE,moveFunc);
			videoHolder.addEventListener(MouseEvent.MOUSE_UP, mUp);
			clickFirst = true;
		}
		
		public function moveFunc(e:MouseEvent):void
		{

			if (clickFirst)
			{
				
				childIndex++;
				childMc = new Shape  ;
				childHolder = new MovieClip  ;
				childHolder.name = "child" + childIndex;
				childMc.name = "child";
				childHolder.x = mouseX;
				childHolder.y = mouseY;
				childHolder.addChild(childMc);
				frameChild.addChild(childHolder);
				childMc.graphics.lineStyle(3,0xFF0000, 1);
				valX = childMc.mouseX;
				valY = childMc.mouseY;
				childMc.graphics.moveTo(childHolder.mouseX, childHolder.mouseY);
				childHolder.addEventListener(MouseEvent.MOUSE_DOWN, tool.select);
			
			}
			clickFirst = false;
			 var tempMain:MovieClip = frameChild.getChildByName("child" + childIndex) as MovieClip;
			 var temp:Shape = tempMain.getChildByName("child") as Shape;
			
			//-- set the color from color picker
		
			switch (drawMode)
			{
				case "tool1" :
					temp.graphics.clear();
					temp.graphics.lineStyle(3,drawingColor, 1);
					temp.graphics.moveTo(valX, valY);
					temp.graphics.lineTo(tempMain.mouseX,tempMain.mouseY);
					break;
		
				case "tool2" :
					temp.graphics.clear();
					temp.graphics.lineStyle(3,drawingColor, 1);
					temp.graphics.drawEllipse(valX, valY, tempMain.mouseX-valX, tempMain.mouseY-valY);
					temp.graphics.endFill();
					break;
		
				case "tool3" :
					temp.graphics.clear();
					temp.graphics.lineStyle(3,drawingColor, 1);
					temp.graphics.drawRect(valX, valY, tempMain.mouseX-valX, tempMain.mouseY-valY);
					temp.graphics.endFill();
		
					break;
		
					
				case "tool4" :
					temp.graphics.lineStyle(3,drawingColor,1);
					temp.graphics.lineTo(tempMain.mouseX, tempMain.mouseY);
		
					break;
					
				case "tool5" :
					temp.graphics.clear();
					temp.graphics.lineStyle(3,drawingColor, 1);
					temp.graphics.moveTo(valX, valY);
					temp.graphics.lineTo(tempMain.mouseX,tempMain.mouseY);
					addArrow("right", tempMain, temp, e);
					break;
					
				case "tool6" :
					temp.graphics.clear();
					temp.graphics.lineStyle(3,drawingColor, 1);
					temp.graphics.moveTo(valX, valY);
					temp.graphics.lineTo(tempMain.mouseX,tempMain.mouseY);
					addArrow("right", tempMain, temp, e);
					break;
				case "tool7" :
					temp.graphics.clear();
					temp.graphics.lineStyle(3,drawingColor, 1);
					temp.graphics.moveTo(valX, valY);
					temp.graphics.lineTo(tempMain.mouseX,tempMain.mouseY);
					addAngleText(tempMain);					 
					break;
				
				
			}
		}
		private function addText():void
		{
			childIndex++;
			var newText:TextField = new TextField();
			newText.name = "text" + childIndex;
			newText.border = true;
			newText.borderColor = drawingColor;
			newText.type = TextFieldType.INPUT;
			newText.x = frameChild.mouseX;
			newText.y = frameChild.mouseY;
			newText.multiline = true;
			frameChild.addChild(newText);
			stage.focus = newText;
			newText.height = 30;
			newText.autoSize = TextFieldAutoSize.LEFT;
			newText.defaultTextFormat = getTextFormat();

		}
		//public function enableToolsPanel():void
		//{
			
		//}
		private function getTextFormat():TextFormat
		{
			newFormat.color = drawingColor;
			newFormat.font = "Arial";
			newFormat.size = 12;
			return newFormat;
		}
		private function addArrow(type:String, mc:MovieClip, shp:Shape, e:MouseEvent):void
		{
			var myAtan2:Number = Math.atan2(childMc.mouseY - valY, childMc.mouseX - valX);
			//trace(mc.y,mc.x)
			
			if(mc.getChildByName("arrow") == null)
			{
				
				var ap:ArrowPoint = new ArrowPoint();
				ap.name = "arrow"
				mc.addChild(ap);
				ap.x = mc.mouseX;
				ap.y = mc.mouseY;
				ap.scaleY = -1;
				ap.rotation = (myAtan2/Math.PI) * 180;
				
			} else {
				
				//myAtan2 = Math.atan2(e.stageY - mCircle.y, e.stageX - mCircle.x);
				
				
				mc.getChildByName("arrow").x = mc.mouseX;
				mc.getChildByName("arrow").y = mc.mouseY;
				mc.getChildByName("arrow").rotation = (myAtan2/Math.PI) * 180;
			}
		}
		private function addAngleText(mc):void
		{
			var myAtan2:Number = Math.atan2(childMc.mouseY - valY, childMc.mouseX - valX);
			
			var angle:Number =  Math.abs(Number((myAtan2/Math.PI) * 180));
			if(angle > 90)
			{
				angle = 90-(angle%90)
			}
			if(mc.getChildByName("text") == null)
			{
				var angleText:TextField = new TextField();
				angleText.name = "text";
				angleText.x = valX;
				angleText.y =  valY;
				angleText.multiline = true;
				
				angleText.height = 20;
				angleText.mouseEnabled = false;
				angleText.autoSize = TextFieldAutoSize.LEFT;
				var textFormat:TextFormat = new TextFormat();
				textFormat.color = 0xFF0000;
				textFormat.font = "Arial";
				textFormat.size = 15;
				angleText.defaultTextFormat = textFormat;
				
				angleText.text = angle.toFixed(3)+"°";					
				mc.addChild(angleText);
				
				
				
			} else {
				
				mc.getChildByName("text").text =  angle.toFixed(3)+"°";
				if(myAtan2 > 0)
				{
					mc.getChildByName("text").y = valY-20;	
				}else {
					mc.getChildByName("text").y = valY;
				}
				
				
			}
		}
		public function mUp(e:MouseEvent):void
		{
			videoHolder.removeEventListener(MouseEvent.MOUSE_MOVE,moveFunc);
			videoHolder.removeEventListener(MouseEvent.MOUSE_UP, mUp);
			if(!textToolSelected)
			{
				currChild = Sprite(e.currentTarget);
			} 
			
			
			if (clickFirst && currChild.name != "videoHolder")
			{
				
			}
			else
			{
				videoHolder.mouseChildren = true;
			}
		
		}


		public function drawToolClicked(evt:MouseEvent):void
		{
			
			textToolSelected = false;
			tool.setTarget(null);
			deleteTool.mouseEnabled = false;
			deleteTool.alpha = .3;
			
			try
			{
				frameChild.removeChild(tool);
			}catch(e){
				
			}
			if(curDrawingTool != null)
			{
				curDrawingTool.gotoAndStop(1)
				curDrawingTool.mouseEnabled = true;
				curDrawingTool.icons.transform.colorTransform = colorNormal;
			}
			curDrawingTool = evt.target as MovieClip;
			curDrawingTool.gotoAndStop(2);
			curDrawingTool.mouseEnabled = false;
			drawMode = evt.currentTarget.name;
		}

		public function toolClicked(evt:MouseEvent):void
		{
			
			textToolSelected = false;
			
			if(curDrawingTool != null)
			{
				curDrawingTool.gotoAndStop(1)
				curDrawingTool.mouseEnabled = true;
				curDrawingTool.icons.transform.colorTransform = colorNormal;
			}
			curDrawingTool = null;
			drawMode = "";
			if(evt.target.name == "tool8")
			{
				if(videoZoomLevel < 3)
				{
					videoClip.scaleX += .2;
					videoClip.scaleY += .2;
					videoZoomLevel +=1;
				}

			}
			else if(evt.target.name == "tool9")
			{
				if(videoZoomLevel > 0)
				{
					videoClip.scaleX -= .2;
					videoClip.scaleY -= .2;
					videoZoomLevel -=1;
				}
			}
			else if(evt.target.name == "tool10")
			{
				videoClip.rotation += 90;
				
			}
			else if(evt.target.name == "tool11")
			{
				videoClip.scaleX = -videoClip.scaleX;
				
			}
			else if(evt.target.name == "tool12")
			{
				textToolSelected = true;
				
			}else if(evt.target.name == "tool14")
			{
				curDrawingTool = evt.target as MovieClip;
				curDrawingTool.gotoAndStop(2);
				curDrawingTool.mouseEnabled = false;
				deleteTool.mouseEnabled = true;
				deleteTool.alpha = 1;
				frameChild.addChild(tool);
				
			}
		}

		
		private function checkButtonType(btn:MovieClip)
		{
			
			if(btn.buttonType == "pushButton")
			{
				
				btn.mouseEnabled = true;
				btn.gotoAndStop(1);
				btn.icons.transform.colorTransform = colorNormal;
			}
			else{
				btn.gotoAndStop(2);
				
			}
			currentToolSelected = btn.name;
		}


//-------clear Board -----//

		public function clearFrameChild()
		{
			
				videoHolder.removeChild(frameChild);
			
		}


	}
	
}
